package cate.game.role.res.formations;

import cate.common.table.res.formations.row.FormationsAttrRow;
import cate.common.table.res.formations.row.FormationsBaseRow;
import cate.common.util.GameResult;
import cp.solution.util.str.Str;
import cate.game.attr.FightAttr;
import cate.game.res.CostSelect;
import cate.game.res.RewardShow;
import cate.game.role.RoleDependent;
import cate.game.role.res.formations.msg.SingleFormationDataResp;
import cate.game.role.task.TaskItem;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@NoteClass("单个阵法的数据")
public class SingleFormation extends RoleDependent {
    @NoteField("阵法id")
    public int tid;
    @NoteField("是否激活")
    public boolean active;

    public SingleFormation() {

    }

    public SingleFormation(int tid) {
        this.tid = tid;
    }

    public GameResult<Void> active(Map<Integer, CostSelect> csMap, List<TaskItem> doneTaskIds) {
        GameResult<Void> r = new GameResult<>();
        if (active) {
            return r.fail("该阵型已经激活了");
        }
        FormationsBaseRow baseRow = getRow();
        if (baseRow == null) {
            return r.fail("无法激活该阵型");
        }
        if (StringUtils.isNotBlank(baseRow.taskIdStr)) {
            for (Integer taskId : Str.str2IntList(baseRow.taskIdStr, "#")) {
                boolean done = false;
                for (TaskItem item : doneTaskIds) {
                    if (item.tid == taskId) {
                        if (item.done && item.rewardGot) {
                            done = true;
                        }
                        break;
                    }
                }
                if (!done) {
                    return r.fail("请先完成任务并领取奖励");
                }
            }
        }
        String heroConsumeStr = "";
        if (StringUtils.isNotBlank(baseRow.activeCostGeneralHeroStr)) {
            heroConsumeStr += baseRow.activeCostGeneralHeroStr;
        }
        if (StringUtils.isNotBlank(baseRow.activeCostSpeHeroStr)) {
            if (StringUtils.isNotBlank(heroConsumeStr)) {
                heroConsumeStr += "#";
            }
            heroConsumeStr += baseRow.activeCostSpeHeroStr;
        }
        GameResult<RewardShow> returnRes = role.getGame().train.hero.checkAndConsume(role, csMap, heroConsumeStr, baseRow.activeCostItemStr);
        if (!returnRes.ok()) {
            return r.fail(returnRes);
        }
        if (!returnRes.data.isEmpty()) {
            role.getGame().notice.dialog(role, returnRes.data);
        }
        active = true;
        noticeUpdate();
        return r;
    }

    public void noticeUpdate() {
        role.sendNow(new SingleFormationDataResp(this));
    }

    @JsonIgnore
    private FormationsBaseRow getRow() {
        return role.getGame().table.formations.base.get(tid);
    }

    private FormationsAttrRow getAttrRow(int level) {
        return role.getGame().table.formations.attr.getRow(tid, level);
    }

    public Map<Byte, FightAttr> getPosAttrs(int level) {
        Map<Byte, FightAttr> map = new HashMap<>();
        FormationsAttrRow attrRow = getAttrRow(level);
        if (attrRow == null) {
            return map;
        }
        if (StringUtils.isNotBlank(attrRow.attr1)) {
            map.put((byte) 1, new FightAttr(attrRow.attr1));
        }
        if (StringUtils.isNotBlank(attrRow.attr2)) {
            map.put((byte) 2, new FightAttr(attrRow.attr2));
        }
        if (StringUtils.isNotBlank(attrRow.attr3)) {
            map.put((byte) 3, new FightAttr(attrRow.attr3));
        }
        if (StringUtils.isNotBlank(attrRow.attr4)) {
            map.put((byte) 4, new FightAttr(attrRow.attr4));
        }
        if (StringUtils.isNotBlank(attrRow.attr5)) {
            map.put((byte) 5, new FightAttr(attrRow.attr5));
        }
        if (StringUtils.isNotBlank(attrRow.attr6)) {
            map.put((byte) 6, new FightAttr(attrRow.attr6));
        }
        return map;
    }

    public boolean isActive() {
        return active;
    }
}
